package com.kuyou.ddhm_tv.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kuyou.ddhm_tv.MainActivity;
import com.kuyou.ddhm_tv.R;
import com.kuyou.ddhm_tv.adapter.CategoryAdapter;
import com.kuyou.ddhm_tv.adapter.ControllerAdapter;
import com.kuyou.ddhm_tv.bean.HomeItemBean;
import com.kuyou.ddhm_tv.utils.BsJudge;

import java.util.ArrayList;
import java.util.List;

public class FController extends Fragment {
    ControllerAdapter adapter;

    RecyclerView rlController;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.f_controller, container, false);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initHomeItem();
    }

    public void initView(View view){
        rlController = view.findViewById(R.id.rl_controller);
    }

    public void initHomeItem(){
        RecyclerView.LayoutManager  layoutManager = new LinearLayoutManager(getContext());
        rlController.setLayoutManager(layoutManager);
        adapter = new ControllerAdapter(getContext());
        adapter.setList(getHomeList());
        adapter.setOnItemClick(new ControllerAdapter.OnItemClick() {
            @Override
            public void OnClik(HomeItemBean homeItemBean) {
                if(homeItemBean.fragment==null){
                    Toast.makeText(getContext(), "暂未开放", Toast.LENGTH_SHORT).show();
                    return;
                }

                if(homeItemBean.id == 5){

                    getActivity().finish();
                    System.exit(0);
                }

                Bundle bundle = new Bundle();


                homeItemBean.fragment.setArguments(bundle);
                ((MainActivity)getActivity()).translateCategoryFragment(homeItemBean.fragment);

            }
        });
        rlController.setAdapter(adapter);
    }

    public List<HomeItemBean> getHomeList(){
        List<HomeItemBean> list = new ArrayList<>();

        list.add(new HomeItemBean(1,R.mipmap.equipm,"设备管理",null));
        list.add(new HomeItemBean(2,R.mipmap.setting,"设置",null));
        list.add(new HomeItemBean(3,R.mipmap.hisrec,"历史记录",null));
        list.add(new HomeItemBean(4,R.mipmap.about,"关于",null));
        list.add(new HomeItemBean(5,R.mipmap.exit,"退出",null));


        return list;
    }
}
